//
//  GFIntroCell.m
//  GoodFood
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFIntroCell.h"

@interface GFIntroCell ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *titleImage;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIButton *button;
@end

@implementation GFIntroCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self creatScrollView];
        
        self.titleImage = [[UIImageView alloc] init];
        [self.scrollView addSubview:self.titleImage];
        self.titleImage.contentMode = UIViewContentModeScaleAspectFill;

        
        self.titleLabel = [[UILabel alloc] init];
        [self.scrollView addSubview:self.titleLabel];

        
        self.contentLabel = [[UILabel alloc] init];
        [self.scrollView addSubview:self.contentLabel];
        self.contentLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
        self.contentLabel.numberOfLines = 0;

        
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.scrollView addSubview:self.button];
        [self.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

        
    }
    return self;
}



/* 创建ScrollView */
- (void)creatScrollView {
    self.scrollView = [[UIScrollView alloc] init];
    
    self.scrollView.contentInset = UIEdgeInsetsMake(WIDTH / 375 * 250, 0, 0, 0);
    self.scrollView.delegate = self;
    
    [self.contentView addSubview:self.scrollView];

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    //    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y;//加上导航栏高度,第一次是-200
    //    NSLog(@"y2 === %f",y);
    
    if (y < - WIDTH / 375 * 250) {
        CGRect frame = self.titleImage.frame;
        frame.origin.y = y;//imageView的frame是不断往上偏移
        frame.size.height =  -y;//tablview向下偏移了多少,高度就增加多少
        self.titleImage.frame = frame;
    }
    
}


#pragma mark _ button点击方法(协议传值)
- (void)buttonClicked:(UIButton *)button {
    [self.delegate sendMethodCode:self.introModel.food_code];
    
}

- (void)useLayoutSubViews {
    
    [self layoutSubviews];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat h = [GFCalculateWidthOrHeigth stringHeight:self.contentLabel.text withWidth:WIDTH - WIDTH / 375 * 40 withFontDictionary:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    
    //    NSLog(@"%@", self.contentLabel.text);
    self.scrollView.frame = self.contentView.frame;
    
    CGFloat sch;
    
    if (h + WIDTH / 375 * 150 > HEIGHT - WIDTH / 375 * 250) {
        
        sch = h + WIDTH / 375 * 150;
    } else {
        
        sch = HEIGHT - WIDTH / 375 * 250 + 1;
    }
    
    self.scrollView.contentSize = CGSizeMake(WIDTH, sch);
    
    self.titleImage.frame = CGRectMake(0, -WIDTH / 375 * 250, WIDTH, WIDTH / 375 * 250);
    
    self.titleLabel.frame = CGRectMake(WIDTH / 375 * 20, 0, WIDTH - WIDTH / 375 * 40, WIDTH / 375 * 50);
    self.titleLabel.font = [UIFont systemFontOfSize:25];
    self.titleLabel.textColor = [UIColor orangeColor];
    
    self.contentLabel.frame = CGRectMake(WIDTH / 375 * 20,WIDTH / 375 * 50, WIDTH - 40, h);
    self.contentLabel.font = [UIFont systemFontOfSize:16];
    
    //    NSLog(@"%@", self.introModel.food_code);
    
    if (self.introModel.food_code != nil) {
        
        [self.scrollView addSubview:self.button];
        
        self.button.frame = CGRectMake(0, 0, WIDTH / 375 * 150, WIDTH / 375 * 50);
        self.button.center = CGPointMake(WIDTH / 2, h + WIDTH / 375 * 100);
        self.button.backgroundColor = [UIColor lightGrayColor];
        self.button.layer.cornerRadius = 10;
        
        
    } else {
        
        [self.button removeFromSuperview];
        
    }
    
    
}

- (void)setIntroModel:(GFIntroModel *)introModel {
    
    if (_introModel != introModel) {

        _introModel = introModel;
    }
    if (introModel.image_url) {
        [self.titleImage setImageWithURL:[NSURL URLWithString:introModel.image_url] placeholderImage:[UIImage imageNamed:@"category_1"]];
    }

    
    self.titleLabel.text = introModel.food_name;
    
    self.contentLabel.text = introModel.descri;
    
    [self.button setTitle:@"查看详情" forState:UIControlStateNormal];
    
}


@end
